/**
 * @author 929467350
 * @github https://github.com/929467350
 * 用于选择本地图片支持手机摄像头拍照 并且修正为正向图片,并缩放到指定大小;支持多选图片
 */
( function ( global, factory ) {
    typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
        typeof define === 'function' && define.amd ? define( factory ) :
            ( global = global || self, global.ImageSelect = factory() );
}( this, ( function () {
    'use strict';


    var ImageSelect = /** @class */ function () {
        function ImageSelect () {
            var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

            var that = this;
            this.ua = function () {
                var u = window.navigator.userAgent;
                return {
                    ios: !!u.match( /\(i[^;]+;( U;)? CPU.+Mac OS X/ ),
                    iosv: u.substr( u.indexOf( 'iPhone OS' ) + 9, 3 )
                };
            }();
            this.el = params.el || function () {
                var input = document.createElement( 'input' );
                input.type = 'file';
                input.accept = params.accept || 'image/*,video/*';
                input.multiple = params.multiple || false;
                return input;
            }();
            this.handler = params.handler || function ( e ) { };
            this.handler.bind( this );
            this.size = params.size || 750;
            this.type = params.type || 'jpeg';
            this.canvas = document.createElement( 'canvas' );
            this.ctx = this.canvas.getContext( '2d' );
            this.el.addEventListener( 'change', function () {
                return that._changeHandler( that.el.files );
            }, false );
        }
        ImageSelect.prototype._changeHandler = function ( files ) {
            var that = this;
            if ( files.length <= 0 ) return this;
            for ( var i = 0; i < files.length; i++ ) {
                ( function ( i ) {
                    var f = new FileReader();
                    f.onload = function () {
                        var src = this.result;
                        var img = new Image();
                        img.src = src;
                        img.onload = function () {
                            EXIF.getData( img, function () {
                                var Orientation = EXIF.getTag( this, 'Orientation' ) || 0;

                                var imgW = img.width;
                                var imgH = img.height;
                                var imgW2,imgH2,tmpImgW,tmpImgH;

                                if ( imgW >= imgH ) {
                                    imgW2 = that.size;
                                    imgH2 = imgW2 * imgH / imgW;
                                    tmpImgW = imgW;
                                    tmpImgH = imgH;
                                } else {
                                    imgH2 = that.size;
                                    imgW2 = imgH2 * imgW / imgH;
                                    tmpImgW = imgH;
                                    tmpImgH = imgW;
                                }

                                var _w, _h, _r;
                                switch ( Orientation ) {
                                    case 3:
                                        _w = imgW2;
                                        _h = imgH2;
                                        _r = 180;
                                        break;
                                    case 6:

                                        if ( that.ua.ios && parseInt( that.ua.iosv ) >= 8 ) {
                                            _r = 0;
                                            _w = imgW2;
                                            _h = imgH2;
                                        } else {
                                            _r = 90;
                                            _w = imgH2;
                                            _h = imgW2;
                                        }
                                        break;
                                    case 8:
                                        _w = imgH2;
                                        _h = imgW2;
                                        _r = 270;
                                        break;
                                    default:
                                        _w = imgW2;
                                        _h = imgH2;
                                        _r = 0;
                                        break;
                                }

                                that.canvas.width = _w;
                                that.canvas.height = _h;
                                that.ctx.clearRect( 0, 0, _w, _h );
                                that.ctx.translate( _w / 2, _h / 2 );
                                that.ctx.rotate( _r * Math.PI / 180 );

                                if ( 3260 < tmpImgW || tmpImgH > 2440 ) {
                                    if ( that.ua.ios ) {
                                        if ( parseInt( that.ua.iosv ) >= 8 ) {
                                            that.ctx.drawImage( img, 0, 0, imgW, imgH, -imgW2 / 2, -imgH2 / 2, imgW2, imgH2 );
                                        } else {
                                            that.ctx.drawImage( img, 0, 0, imgW / 2, imgH / 2, -imgW2 / 2, -imgH2 / 2, imgW2, imgH2 );
                                        }
                                    } else {
                                        that.ctx.drawImage( img, 0, 0, imgW, imgH, -imgW2 / 2, -imgH2 / 2, imgW2, imgH2 );
                                    }
                                } else {
                                    that.ctx.drawImage( img, -imgW2 / 2, -imgH2 / 2, imgW2, imgH2 );
                                }

                                that.handler.call( this, {
                                    img: that.canvas.toDataURL( 'image/' + that.type, 1 )
                                } );
                                f = null;
                            } );
                        };
                    };
                    f.readAsDataURL( files[i] );
                } )( i );
            }
            return this;
        };
        ImageSelect.prototype.select = function () {
            if ( this.el ) {
                this.el.click();
                this.el.value = '';
            }
            return this;
        };
        return ImageSelect;
    }();

    return ImageSelect;

} ) ) );
